var glNoteDirective = angular.module('glNoteDirective', []);
glNoteDirective.directive('confirmPwd',[function(){
	return {
		require: 'ngModel',
		scope:{
			pwd: '=confirmPwd'
		},
		link: function(scope, elem, attr, ctrl){
			ctrl.$validators.confirmPwd = function(modelValue, viewValue){
				return modelValue == scope.pwd;
			}
			scope.$watch("pwd", function() {
	          ctrl.$validate();
	        });
		}
	};
}])
.directive('checkUserAvail', ['myConfig','$q','CheckUserService',function(myConfig, $q, CheckUserService){
	return {
		require: 'ngModel',
		scope:{},
		link: function(scope, elem, attr, ctrl){
			ctrl.$asyncValidators.checkUserAvail = function(modelValue, viewValue){
				if (ctrl.$isEmpty(modelValue)) {
		          // consider empty model valid
		          return $q.when();
		        }
				var def = $q.defer();
				CheckUserService.get({username:modelValue}, function(value, responseHeaders){
					if(value){
						if(value.status == myConfig.success){
							def.resolve();
						}else{
							def.reject();
						}
					}else{
						def.reject();
					}
					
				},function(){
					def.reject();
				});
				return def.promise;
			}
		}
	};
}]);